#!/usr/bin/python3.8.9
# -*- coding: utf-8 -*-

# @Author  : Tina Yu
# @Time    : 2021-12-1 18:53
import logging

from utils.get_log_file_path import *


class MyLogger(object):
    _log_file_level = logging.INFO
    _log_cli_level = logging.DEBUG

    def __init__(self, name=None, level=logging.DEBUG, file=None):
        """
        指定保存日志的文件路径，日志级别，以及调用文件
        将日志存入到指定的文件中
        :param name
        """
        # super().__init__(name, level)
        # 创建一个logger
        self.logger = logging.getLogger(name)
        # 设置输出级别
        self.logger.setLevel(level)
        try:
            # 创建输出到文件的处理器
            fh = logging.FileHandler(file, encoding='utf-8')
            fh.setLevel(self._log_file_level)

            # 创建输出到控制台的处理器
            ch = logging.StreamHandler()
            ch.setLevel(self._log_cli_level)

            # 定义handle的输出格式
            # todo 这里打印信息的文件名称（filename）获取的不正确，当前总是my_logger.py
            formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(message)s [%(filename)s:%(lineno)s]')
            # formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(message)s [%(funcName)s:%(lineno)s]')
            fh.setFormatter(formatter)
            ch.setFormatter(formatter)

            # 给logger添加handler
            self.logger.addHandler(fh)
            self.logger.addHandler(ch)
        except Exception as e:
            print("输出日志失败！ %s" % e)

    # # 日志接口，用户只需调用这里的接口即可，可根据需要定义更多接口
    # def info(self, msg):
    #     self.logger.info(msg)
    #     return
    #
    # def warning(self, msg):
    #     self.logger.warning(msg)
    #     return
    #
    # def error(self, msg):
    #     self.logger.error(msg)
    #     return
    #
    # def debug(self, msg):
    #     self.logger.debug(msg)
    #     return
    #
    # def critical(self, msg):
    #     self.logger.critical(msg)
    #     return
    #
    # # 定义一个函数，回调logger实例
    # def get_log(self):
    #     return self.logger


# 实例化
log = MyLogger("my_log", file=get_log_file_path()).logger

# # 用于调试
# if __name__ == '__main__':
#     log = MyLogger("my_log", file=get_log_file_path()).logger
#     log.info('ddddd')
#     log = MyLogger("my_log", file=get_log_file_path())
#     log.debug('debug信息')
#     log.info('This is info')
#     log.warning('This is warning')
#     log.error('This is error')
#     log.critical('critical级别的信息')
#     #log.info(log.get_log())
